package com.linkin.homepage.ui.holder;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.linkin.homepage.R;
import com.linkin.homepage.base.Colors;
import com.linkin.homepage.base.recycleview.RCWrapperAdapter;
import com.linkin.homepage.base.util.ScreenUtils;

public class ItemDecoration extends RecyclerView.ItemDecoration {

    private Paint mPaint = null;
    private Rect mBounds = new Rect();
    private Rect mTempRect = new Rect();

    private int mDividerMarginLeft = 0;
    private int mDividerMarginRight = 0;
    private int mSpecialSpace = 0;

    public ItemDecoration(Context context) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mDividerMarginLeft = mDividerMarginRight = context.getResources().getDimensionPixelSize(R.dimen.common_margin);
        mSpecialSpace = ScreenUtils.dip2px(10);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view,
                               RecyclerView parent, RecyclerView.State state) {
        outRect.top = 0;
        outRect.bottom = 0;
        int position = parent.getChildAdapterPosition(view);
        RecyclerView.Adapter adapter = parent.getAdapter();
        if (adapter == null) {
            return;
        }

        int itemCount;
        if (adapter instanceof RCWrapperAdapter) {
            RecyclerView.Adapter realAdapter = ((RCWrapperAdapter) adapter).getInnerAdapter();
            if (realAdapter == null) {
                return;
            }
            position = position - ((RCWrapperAdapter) adapter).getHeaderViewsCount();
            itemCount = realAdapter.getItemCount();
            adapter = realAdapter;
        } else {
            itemCount = adapter.getItemCount();
        }

        if (position < 0 || position >= itemCount) {
            return;
        }

        int viewType = adapter.getItemViewType(position);
        if (viewType == 17 || viewType == 18 || viewType == 19) {
            if (position == 0) {
                outRect.top = mSpecialSpace;
            } else {
                viewType = adapter.getItemViewType(position - 1);
                if (viewType != 17 && viewType != 18 && viewType != 19) {
                    outRect.top = mSpecialSpace;
                }
            }
            outRect.bottom = mSpecialSpace;
            return;
        }

        if (position < itemCount - 1) {
            viewType = adapter.getItemViewType(position + 1);
            if (viewType == 17 || viewType == 18) {
                return;
            }
        }
        outRect.bottom = 1;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (parent.getLayoutManager() != null) {
            drawVertical(c, parent, state);
        }
    }

    private void drawVertical(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        canvas.save();
        final int left;
        final int right;

        //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
        if (parent.getClipToPadding()) {
            left = parent.getPaddingLeft();
            right = parent.getWidth() - parent.getPaddingRight();
            canvas.clipRect(left, parent.getPaddingTop(), right,
                    parent.getHeight() - parent.getPaddingBottom());
        } else {
            left = 0;
            right = parent.getWidth();
        }

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            parent.getDecoratedBoundsWithMargins(child, mBounds);

            mTempRect.setEmpty();
            getItemOffsets(mTempRect, child, parent, state);

            if (mTempRect.top > 0) {
                final int top = mBounds.top + Math.round(child.getTranslationY());
                final int bottom = top + mTempRect.top;
                if (mTempRect.top == mSpecialSpace) {
                    mPaint.setColor(0xfff8f8f8);
                    canvas.drawRect(left, top, right, bottom, mPaint);
                } else {
                    mPaint.setColor(0xfff2f2f2);
                    canvas.drawRect(left + mDividerMarginLeft, top, right - mDividerMarginRight, bottom, mPaint);
                }
            }

            if (mTempRect.bottom > 0) {
                final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
                final int top = bottom - mTempRect.bottom;
                if (mTempRect.bottom == mSpecialSpace) {
                    mPaint.setColor(0xfff8f8f8);
                    canvas.drawRect(left, top, right, bottom, mPaint);
                } else {
                    mPaint.setColor(Colors.COLOR_EEE);
                    canvas.drawRect(left + mDividerMarginLeft, top, right - mDividerMarginRight, bottom, mPaint);
                }
            }
        }
        canvas.restore();
    }
}
